﻿using Bangle.BLL;
using Bangle.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Customer_ProgressRecord : BasePage
{
    UtilityBLL u_bll = new UtilityBLL();
    T_ClassificationBLL class_bll = new T_ClassificationBLL();
    T_ProgressRecord model = new T_ProgressRecord();
    T_ProgressRecordBLL bll = new T_ProgressRecordBLL();
    T_CustomerBLL customer_bll = new T_CustomerBLL();

    #region 属性
    /// <summary>
    /// CustomerId
    /// </summary>
    private string CustomerId
    {
        get
        {
            return Request.QueryString["CustomerId"] == null ? Guid.Empty.ToString() : Request.QueryString["CustomerId"].ToString();
        }
    }
    /// <summary>
    /// ProgressRecordId
    /// </summary>
    private string ProgressRecordId
    {
        get
        {
            return Request.QueryString["ProgressRecordId"] == null ? Guid.Empty.ToString() : Request.QueryString["ProgressRecordId"].ToString();
        }

    }
    /// <summary>
    /// Action
    /// </summary>
    private string Action
    {
        get
        {
            return Request.QueryString["Action"] == null ? "Add" : Request.QueryString["Action"].ToString();
        }
    }
    #endregion

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        setUserControlStatus();

        if (!IsPostBack)
        {
            BindDropDownList();

            BindRepeaterData();

            this.txt_RecordTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }
    }

    #endregion

    #region 设置用户权限
    private void setUserControlStatus()
    {
        //检验用户权限
        base.CheckUserPermission("002001");
        this.btn_Save.Enabled = base.CheckUserPermissionOperation("002001", "Add") || base.CheckUserPermissionOperation("002001", "Edit");
    }
    #endregion

    #region 绑定数据

    private void BindDropDownList()
    {
        T_Customer customer_model = customer_bll.GetModel(Guid.Parse(CustomerId));
        if (customer_model != null)
        {
            this.lbl_CustomerId.Text = customer_model.CustomerName;
        }

        //类型
        List<T_Classification> class_list = class_bll.GetModelList("1=1");

        //记录类型
        int recorder_id = class_list.Find(o => o.ClassificationCode == "RecordType").ClassificationId;
        List<T_Classification> series_list = class_list.FindAll(o => o.ParentId == recorder_id);
        this.ddl_RecordType.DataTextField = "ClassificationName";
        this.ddl_RecordType.DataValueField = "ClassificationId";
        this.ddl_RecordType.DataSource = series_list;
        this.ddl_RecordType.DataBind();
        this.ddl_RecordType.Items.Insert(0, new ListItem("----请选择----", "0"));


        //记录类型
        int progress_id = class_list.Find(o => o.ClassificationCode == "ProgressType").ClassificationId;
        List<T_Classification> progress_list = class_list.FindAll(o => o.ParentId == progress_id);
        this.ddl_ProgressType.DataTextField = "ClassificationName";
        this.ddl_ProgressType.DataValueField = "ClassificationId";
        this.ddl_ProgressType.DataSource = progress_list;
        this.ddl_ProgressType.DataBind();
        this.ddl_ProgressType.Items.Insert(0, new ListItem("----请选择----", "0"));
    }

    private void BindRepeaterData()
    {
        PaginateCtl.PageSize = 10;
        int pageIndex = PaginateCtl.CurrentPageIndex;
        int pageSize = PaginateCtl.PageSize;

        UtilityBLL ubll = new UtilityBLL();

        int rCount = 0;
        string swhere = "CustomerId='" + CustomerId + "'";
        DataTable dt = ubll.GetListByPage("View_T_ProgressRecord", "*", "CreateTime", swhere, pageSize, pageIndex, 1, out rCount);

        //返回记录条数
        PaginateCtl.RecordCount = rCount;
        //设置记录总数的显示文本(此方法必须在设置PaginateCtl.RecordCount之后调用)
        TextUtility.SetPaginateCtlText(PaginateCtl);

        //绑定数据源
        RepeaterList.DataSource = dt;
        RepeaterList.DataBind();
    }
    #endregion

    #region 提交
    protected void btn_Save_Click(object sender, EventArgs e)
    {

        model = new T_ProgressRecord();

        model.CustomerId = Guid.Parse(CustomerId);
        model.ProgressTypeId = Convert.ToInt32(this.ddl_ProgressType.SelectedValue);
        model.RecordTypeId = Convert.ToInt32(this.ddl_RecordType.SelectedValue);
        model.RecordTime = this.txt_RecordTime.Text.Trim();
        model.RecordContent = this.txt_RecordContent.Text.Trim();
        model.NextRecordTime = this.txt_NextRecordTime.Text.Trim();
        model.NextRecordContent = this.txt_NextRecordContent.Text.Trim();
        model.NextRecordRead = 0;
        model.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        model.UpdateUserId = base.CurrentSession.UserID;

        try
        {
            if (Action == "Modify")
            {
                //修改
                bll.Update(model);
            }
            else if (Action == "Add")
            {
                model.ProgressRecordId = Guid.NewGuid();
                model.CreateTime = model.UpdateTime;
                model.CreateUserId = model.UpdateUserId;
                model.Valid = 1;

                //保存
                bll.Add(model);
            }

            base.Redirect("操作成功！", string.Format("ProgressRecord.aspx?CustomerId={0}&ProgressRecordId={1}&Action={2}", CustomerId, ProgressRecordId, "Add"));
        }
        catch (Exception ex)
        {
            base.AlertError(ex.ToString());
        }
    }
    #endregion

    #region 分页
    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PaginateCtl_PageChanged(object sender, EventArgs e)
    {
        BindRepeaterData();
    }
    #endregion
}